package com.hnkywl.sts.action.cwgl;

import com.hnkywl.sts.entity.cwgl.Kmdm;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.xsgl.Kcbj;
import com.hnkywl.sts.service.cwgl.KmdmService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.organ.NbgsService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import jxl.Sheet;
import jxl.Workbook;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-11
 * Time: 下午2:23
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/kmdm")
public class KmdmAaction extends SimpleActionSupport {
    private File upload;

    private Long kmdmgsmcId;

    private NbgsService nbgsService;

    private KmdmService kmdmService;

    private Kmdm kmdm;

    private int page;
    //分页组件 页面大小
    private int rows = 5;

    private Long gsId;

    private Long kmId;

    private String sjcode;

    private String code;

    private String name;

    private YgService ygService;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    @Action("kmdm")
    public String kmdm() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        gsId =  ygService.findYgDetail(YgService.getCurrentYg().getId()).getZggs().getId();
        return SUCCESS;
    }

    @Action("search_kmdm")
    public String searchKmdm() {
        return SUCCESS;
    }

    @Actions({@Action("kmdm_content")})
    public String fkshContent() {
        //进入update页面
        if (kmId != null) {
            kmdm = this.kmdmService.findKmdmDetail(kmId);
        }
        return SUCCESS;
    }

    @Action("update_kmdm")
    public void updateKmdm() {
        try {
            this.kmdmService.updateKmdm(kmdm);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改科目代码成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     getTreeJson
     * 功能描述:   生成树Json串
     * 输入参数:   <@param wzbmId level>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     ccr
     * 创建日期:   13-2-20 下午15:47
     * 修改人:     ccr
     * 修改日期:   13-2-20 下午15:47
     */
    @Action("get_kmdmtreejson")
    public void getTreeJson() {
        if (gsId == null) {
            gsId = YgService.getCurrentYg().getZggs().getId();
        }
        if (request.getParameter("id") == null) {
            StringBuilder sb = new StringBuilder("[{\"id\": \"0\", \"text\": \"科目代码\", \"attributes\": {\"code\": \"null\", \"children\": \"false\"}, \"state\": \"open\",\"children\":");
            String treeJson = "[{\"id\":\"dl:资产类\",\"text\":\"资产类\",\"attributes\":{\"code\":\"null\",\"children\":\"true\"},\"state\":\"closed\"}," +
                    "{\"id\":\"dl:负债类\",\"text\":\"负债类\",\"attributes\":{\"code\":\"null\",\"children\":\"true\"},\"state\":\"closed\"}," +
                    "{\"id\":\"dl:损益类\",\"text\":\"损益类\",\"attributes\":{\"code\":\"null\",\"children\":\"true\"},\"state\":\"closed\"}," +
                    "{\"id\":\"dl:所有者权益类\",\"text\":\"所有者权益类 \",\"attributes\":{\"code\":\"null\",\"children\":\"true\"},\"state\":\"closed\"}," +
                    "{\"id\":\"dl:成本\",\"text\":\"成本\",\"attributes\":{\"code\":\"null\",\"children\":\"true\"},\"state\":\"closed\"}," +
                    "{\"id\":\"dl:共同类\",\"text\":\"共同类\",\"attributes\":{\"code\":\"null\",\"children\":\"true\"},\"state\":\"closed\"}]";
            sb.append(treeJson).append("}]");
            renderHtml(sb.toString());
        } else {
            String[] values = request.getParameter("id").split(":");
            String sjcode = String.valueOf(values[0]);
            String kmdl = String.valueOf(values[1]);
            renderText(kmdmService.getKmbmTreeJson(sjcode, kmdl, gsId));
        }
    }


    @Action("kmdmList")
    public void kmdmList() {
        if (gsId == null) {
            gsId = ygService.findYgDetail(YgService.getCurrentYg().getId()).getZggs().getId();
        }
        this.renderJson(this.kmdmService.findAllKmdm(gsId, name, code, page, rows));
    }

    @Action("findKmdm")
    public void findKmdm() {
        this.renderJson(kmdmService.findKmdm(gsId, code, rows));
    }

    //发现辅助核算是项目的科目代码
    @Action("findKmdmXm")
    public void findKmdmXm() {
        if(gsId==null){
            gsId = ygService.findYgDetail(YgService.getCurrentYg().getId()).getZggs().getId();
        }
        this.renderJson(kmdmService.findKmdmXm(gsId,page, rows));
    }


    @Action("kmdm_detail")
    public String kmdmDetail() {
        kmdm = this.kmdmService.findKmdmDetail(kmId);
        return SUCCESS;
    }

    //科目代码导入界面展示
    @Action("kmdm_upload")
    public String kmdmUpload() {
        return SUCCESS;
    }

    //科目代码导入
    @Action("kmdm_import")
    public void kmdmImport() {
        try {
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
            Workbook book = Workbook.getWorkbook(upload);
            //  获得第一个工作表对象
            Sheet sheet = book.getSheet(0);
            this.kmdmService.deleteKmdmByGs(kmdmgsmcId);
            //获取总行数
            int rows = sheet.getRows();
            //获取总列数
            int columns = sheet.getColumns();
            for (int i = 1; i < rows; i++) {
                Kmdm kmdm = new Kmdm();
                kmdm.setSjcode("");
                kmdm.setNbgs(nbgsService.findNbgsDetail(kmdmgsmcId));
                //编码
                String cell1 = sheet.getCell(0, i).getContents().trim();
                kmdm.setCode(cell1);
                //名称
                String cell3 = sheet.getCell(1, i).getContents().trim();
                kmdm.setName(cell3);
                //助记码
                String cell4 = sheet.getCell(2, i).getContents().trim();
                kmdm.setZjm(cell4);
                //科目方向
                String cell5 = sheet.getCell(3, i).getContents().trim();
                kmdm.setKmfx(cell5);
                //科目属性
                String cell6 = sheet.getCell(4, i).getContents().trim();
                kmdm.setKmsx(cell6);
                //科目大类
                String cell7 = sheet.getCell(5, i).getContents().trim();
                kmdm.setKmdl(cell7);
                //币别
                String cell8 = sheet.getCell(6, i).getContents().trim();
                kmdm.setBb(cell8);
                //受控系统
                String cell9 = sheet.getCell(7, i).getContents().trim();
                kmdm.setSkxt(cell9);
                //货币核算类型
                String cell10 = sheet.getCell(8, i).getContents().trim();
                kmdm.setHbhslx(cell10);
                //辅助核算类型
                String cell11 = sheet.getCell(9, i).getContents().trim();
                kmdm.setFzhslx(cell11);
                //启用往来
                String cell12 = sheet.getCell(10, i).getContents().trim();
                kmdm.setQywl(cell12);
                //现金流量属性
                String cell14 = sheet.getCell(11, i).getContents().trim();
                kmdm.setXjllsx(cell14);
                //借方流量项目
                String cell15 = sheet.getCell(12, i).getContents().trim();
                kmdm.setJfllxm(cell15);
                //贷方流量项目
                String cell16 = sheet.getCell(13, i).getContents().trim();
                kmdm.setDfllxm(cell16);
                //特殊余额科目
                String cell17 = sheet.getCell(14, i).getContents().trim();
                kmdm.setTsyekm(cell17);
                //现金流量备注
                String cell18 = sheet.getCell(15, i).getContents().trim();
                kmdm.setXjllbz(cell18);
                //可以增加下级
                String cell19 = sheet.getCell(16, i).getContents().trim();
                kmdm.setKxzjxj(cell19);
                //是否传递现金流量到下级
                String cell20 = sheet.getCell(17, i).getContents().trim();
                kmdm.setSfcdxjlldxj(cell20);
                this.kmdmService.saveKmdm(kmdm);
            }
            this.kmdmService.createSjcode(kmdmgsmcId);
            this.kmdmService.createSfxj(kmdmgsmcId);
            renderText("科目代码导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage().toString());
        }
    }


    //科目代码导入
    @Action("kmdm_import_wly")
    public void kmdmImportWly() {
        try {
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
            Workbook book = Workbook.getWorkbook(upload);
            //  获得第一个工作表对象
            Sheet sheet = book.getSheet(0);
            this.kmdmService.deleteKmdmByGs(kmdmgsmcId);
            //获取总行数
            int rows = sheet.getRows();
            //获取总列数
            int columns = sheet.getColumns();
            for (int i = 1; i < rows; i++) {
                Kmdm kmdm = new Kmdm();
                kmdm.setSjcode("");
                kmdm.setNbgs(nbgsService.findNbgsDetail(kmdmgsmcId));
                //类型
                String cell0 = sheet.getCell(0, i).getContents().trim();
                kmdm.setKmdl(cell0);
                //编码
                String cell2 = sheet.getCell(2, i).getContents().trim();
                kmdm.setCode(cell2);
                //名称
                String cell3 = sheet.getCell(3, i).getContents().trim();
                kmdm.setName(cell3);
                //助记码
                String cell4 = sheet.getCell(4, i).getContents().trim();
                kmdm.setZjm(cell4);
                //外币币种
                String cell5 = sheet.getCell(5, i).getContents().trim();
                kmdm.setBb(cell5);
                //辅助核算类型
                String cell7 = sheet.getCell(7, i).getContents().trim();
                kmdm.setFzhslx(cell7);
                //帐页格式
                String cell8 = sheet.getCell(8, i).getContents().trim();
                kmdm.setZygs(cell8);
                //余额方向
                String cell9 = sheet.getCell(9, i).getContents().trim();
                kmdm.setKmfx(cell9);
                //受控系统
                String cell10 = sheet.getCell(10, i).getContents().trim();
                kmdm.setSkxt(cell10);
                this.kmdmService.saveKmdm(kmdm);
            }
            this.kmdmService.createKmdl(kmdmgsmcId);
            this.kmdmService.createSjcode(kmdmgsmcId);
            this.kmdmService.createSfxj(kmdmgsmcId);
            this.kmdmService.updateFzhslx(kmdmgsmcId);
            renderText("科目代码导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage().toString());
        }
    }




    //=======================GetterAndSetter===========================================================

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public Long getKmdmgsmcId() {
        return kmdmgsmcId;
    }

    public void setKmdmgsmcId(Long kmdmgsmcId) {
        this.kmdmgsmcId = kmdmgsmcId;
    }

    public Kmdm getKmdm() {
        return kmdm;
    }

    public void setKmdm(Kmdm kmdm) {
        this.kmdm = kmdm;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public String getSjcode() {
        return sjcode;
    }

    public void setSjcode(String sjcode) {
        this.sjcode = sjcode;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Long getKmId() {
        return kmId;
    }

    public void setKmId(Long kmId) {
        this.kmId = kmId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    //========================Setter========================================
    @Autowired
    public void setNbgsService(NbgsService nbgsService) {
        this.nbgsService = nbgsService;
    }

    @Autowired
    public void setKmdmService(KmdmService kmdmService) {
        this.kmdmService = kmdmService;
    }
    @Autowired

    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }
}
